package com.yycwxt.modules.admin.bcf;

import java.util.Map;

/**
 * Created by Mr.Zhang on 2022/9/7 20:43
 */
public class BcfSelectProvider {

    // mybatis 动态sql 方法
    public String bcfSql(Map<String, Object> map) {

        String [] names = (String[]) map.get("storeNames"); // 取值
        String template = """
                , count(case when store_name = '%s' then order_number end) '%s订单数'
                , count(case when store_name = '%s' and subject_code = '1007-01-001' then order_number end) '%s常温品订单数'
                ,   sum(case when store_name = '%s' and subject_code = '1007-01-001' then cost end) '%s常温品包材费'
                , count(case when store_name = '%s' and subject_code = '1007-01-002' then order_number end) '%s冷冻品订单数'
                ,   sum(case when store_name = '%s' and subject_code = '1007-01-002' then cost end) '%s冷冻品包材费'
                """;
        StringBuilder sql = new StringBuilder();
        sql.append("select shop_name '店铺'");
        for (int i = 0; i < names.length; i++) {
            sql.append(String.format(template, names[i], names[i], names[i], names[i], names[i], names[i], names[i], names[i], names[i], names[i]));
        }

        sql.append("""
                , count(case when subject_code = '1007-01' then order_number end) '发货订单数'
                , count(case when subject_code = '1007-01-001' then order_number end) '常温品订单数合计'
                ,   sum(case when subject_code = '1007-01-001' then cost end) '常温品包材费合计'
                , count(case when subject_code = '1007-01-002' then order_number end) '冷冻品订单数合计'
                ,   sum(case when subject_code = '1007-01-002' then cost end) '冷冻品包材费合计'
                ,   sum(case when subject_code = '1007-01-004-01' and remark = '冰袋、保温袋' then cost end) '金社长冰袋保温袋费用'
                ,   sum(case when subject_code = '1007-01-004-02' and remark = '干冰' then cost end) '冷媒干冰'
                """)
                .append("from core_flows ")
                .append("where account_date = " + map.get("zq") + " ")
                .append("and subject_code like '1007-%' ")
                .append("group by shop_name");
        return sql.toString();
    }
}
